package com.sina.shop.manage.service.carousel;

import com.sina.shop.core.exception.client.DataException;
import com.sina.shop.core.exception.server.CreateException;
import com.sina.shop.core.exception.server.ResourceExistsException;
import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.util.Assert;
import com.sina.shop.util.ResultCode;
import com.sina.shop.util.StringUtils;
import com.sina.shop.domain.carousel.Carousel;
import com.sina.shop.manage.dao.carousel.CarouselManageDao;
import com.sina.shop.service.ShopDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.sina.shop.service.ServiceConstant.SHOP_TX;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-11-23-15:31
 */
@Service("carouselManageService")
public class CarouselManageServiceImpl extends ShopDataSourceService implements CarouselManageService {

    @Autowired
    @Qualifier("carouselManageDao")
    private CarouselManageDao carouselManageDao;


    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public int deleteCarousel(Long id) throws DataException, UpdateException {
        Carousel carousel = carouselManageDao.get(Carousel.class, id);
        Assert.assertNotNull(carousel, "Carousel[id=%s] not exists", carousel.getId());

        try {
            return carouselManageDao.deleteCarousel(id);
        } catch (DataAccessException e) {
            LOG.error(" {} delete Carousel error.", Carousel.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public int deleteCarouselTab(Long id) throws DataException, UpdateException {

        Carousel carousel = carouselManageDao.get(Carousel.class, id);
        Assert.assertNotNull(carousel, "Carousel[id=%s] not exists", carousel.getId());

        try {
            return carouselManageDao.deleteCarouselTab(id);
        } catch (DataAccessException e) {
            LOG.error(" {} deleteTab Carousel error.", Carousel.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.REPEATABLE_READ)
    public Carousel updateCarousel(Carousel carousel) throws UpdateException, DataException {
        Carousel c = carouselManageDao.get(Carousel.class, carousel.getId());
        Assert.assertNotNull(c, "Carousel[id=%s] not exists", carousel.getId());

        try {
            c = updateDocumentsFiled(carousel, c);
            carouselManageDao.flush();

            return c;
        } catch (DataAccessException e) {
            LOG.error(" {} update Carousel error.", Carousel.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }

    }

    private Carousel updateDocumentsFiled(Carousel oldCarousel, Carousel newCarousel) throws CreateException {

        if (!StringUtils.isEmpty(oldCarousel.getName())) {
            newCarousel.setName(oldCarousel.getName());
        }

        if (!StringUtils.isEmpty(oldCarousel.getStatus())) {
            newCarousel.setStatus(oldCarousel.getStatus());
        }

        return newCarousel;
    }

    @Override
    @Transactional(value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public Carousel saveCarousel(Carousel carousel) throws CreateException, ResourceExistsException, DataException {
        boolean exists;
        exists = carouselManageDao.isExistsByUnique(Carousel.class, "storageFileName", carousel.getStorageFileName());
        Assert.assertFalse(exists, ResourceExistsException.class, ResultCode.dataExists
                , "Carousel[%s] exists", carousel.getName());

        try {
            carouselManageDao.save(carousel);
            carouselManageDao.flush();
            return carousel;
        } catch (DataAccessException e) {
            LOG.error(" {} save Carousel error.", Carousel.class, e);
            throw new UpdateException(ResultCode.dataAccessError, e.getMessage(), e);
        }
    }

    @Override
    @Transactional(readOnly = true, value = SHOP_TX, isolation = Isolation.READ_COMMITTED)
    public List<Map<String, Object>> getCarouselList(Carousel carousel, Long[] count) {

        if (count != null && count.length > 0) {
            count[0] = carouselManageDao.getCarouselCount(carousel);
        }

        return carouselManageDao.getCarouselList(carousel);
    }
}
